package com.lagou.slqSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private final Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws Exception {
        Executor executor = new SimpleExecutor();
        MappedStatement statement = configuration.getMappedStatementMap().get(statementId);
        List<Object> list = executor.query(configuration, statement, params);
        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> objectList = selectList(statementId, params);
        if (objectList.size() == 1) {
            return (T) objectList.get(0);
        } else if (objectList.size() > 1) {
            throw new RuntimeException("The result is not unique!");
        }
        return null;
    }

    @Override
    public int insert(String statementId, Object... params) throws Exception {
        Executor executor = new SimpleExecutor();
        MappedStatement statement = configuration.getMappedStatementMap().get(statementId);
        return executor.update(configuration, statement, params);
    }

    @Override
    public int update(String statementId, Object... params) throws Exception {
        Executor executor = new SimpleExecutor();
        MappedStatement statement = configuration.getMappedStatementMap().get(statementId);
        return executor.update(configuration, statement, params);
    }

    @Override
    public int delete(String statementId, Object... params) throws Exception {
        Executor executor = new SimpleExecutor();
        MappedStatement statement = configuration.getMappedStatementMap().get(statementId);
        return executor.update(configuration, statement, params);
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className + "." + methodName;
                MappedStatement statement = configuration.getMappedStatementMap().get(statementId);
                switch (statement.getType()){
                    case DELETE:
                        return delete(statementId, args);
                    case INSERT:
                        return insert(statementId, args);
                    case UPDATE:
                        return update(statementId, args);
                    case SELECT:
                        Type returnType = method.getGenericReturnType();
                        if (returnType instanceof ParameterizedType){
                            List<Object> list = selectList(statementId, args);
                            return list;
                        }
                        return selectOne(statementId, args);
                    default:
                        throw new RuntimeException("Unknown sql type");
                }
            }
        });
        return (T) proxyInstance;
    }
}
